
const clearStorage = function() {
    uni.clearStorage({
        success: function(response) {
            console.log(response);
        },
        fail: function(response) {
            console.log(response);
        },
    })
};

const setStorageSync = function(key, data) {
    try {
        uni.setStorageSync(key, data);
    } catch (response) {
        console.log(response);
    }
};

const getStorageSync = function(key) {
    try {
        return uni.getStorageSync(key);
    } catch(response) {
        console.log(response);
    }
};

const removeStorageSync = function(key) {
    try {
        uni.removeStorageSync(key)
    } catch (response) {
        console.log(response);
    }
};



//设置缓存时间
/**
 * @param {Object} key
 * @param {Object} data
 * @param {Object} num 缓存时间秒
 */
const setStorageTime = function(key, data,num) {
    try {
		num = num || 0; 
		let time = parseInt(new Date().getTime()/1000);
		let info = {
			time: time+num,
			data:data
		}
        uni.setStorageSync(key,info);
    } catch (response) {
        console.log(response);
    }
};

const getStorageTime = function(key) {
    try {
		let time = parseInt(new Date().getTime()/1000);
		let info =  uni.getStorageSync(key);
		if( info && (info.time > time || info.time == 0 )){
			return info.data;
		}
        return null;
    } catch(response) {
        console.log(response);
    }
};



export { clearStorage, setStorageSync, getStorageSync, removeStorageSync,getStorageTime, setStorageTime}
